"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : gs_ctl start指定-l FILENAME，当指定文件在数据库实例目录下备机
                是否可以启动成功（参数后指定文件路径）
Create At   : 2021/01/18
Owner       : opentestcase015
Description :
    1.关闭运行正常的集群
    2.使用开启数据库的用户在实例目录下创建文件
    3.使用-l参数并指定文件路径启动数据库（备机执行）
    4.查看gsctl_test.dat文件中是否有记录信息
    5.查看集群状态，备机正常启动
    6.恢复环境
Expect      :
    1.关闭运行正常的集群成功
    2.使用开启数据库的用户在实例目录下创建文件成功
    3.使用-l参数并指定该文件路径启动数据库备机成功
    4.查看gsctl_test.dat文件中，文件中有记录信息
    5.查看集群状态，备机正常启动
    6.成功
History     :
    Modified by opentestcase027 2023/1/9: 优化用例步骤格式及一主两备判断方式
"""

import unittest
import os
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class LogicalReplication(unittest.TestCase):
    def setUp(self):
        LOG.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.PrimaryNode = Node('PrimaryDbUser')
        self.user_node = Node('Standby1DbUser')
        self.sh_standby = CommonSH('Standby1DbUser')
        
    def test_system_internal_tools(self):
        LOG.info('--------step1:关闭运行正常的集群   expect:成功--------')
        is_stop = self.sh_standby.stop_db_cluster()
        self.assertTrue(is_stop)

        LOG.info('--------step2:在实例目录下创建文件  expect:创建文件成功--------')
        excute_cmd = f"touch {macro.DB_INSTANCE_PATH}/gsctl_test.dat; " \
                     f"ls -l {macro.DB_INSTANCE_PATH}/gsctl_test.dat"
        LOG.info(excute_cmd)
        msg = self.user_node.sh(excute_cmd).result()
        LOG.info(msg)
        self.assertNotIn(self.constant.NO_FILE_MSG, msg)

        LOG.info('--------step3:启动数据库  expect:启动数据库备机成功--------')
        excute_cmd = f'''
                    source {macro.DB_ENV_PATH};
                    gs_ctl start -D {macro.DB_INSTANCE_PATH} -M standby \
                    -l {macro.DB_INSTANCE_PATH}/gsctl_test.dat; 
                        '''
        LOG.info(excute_cmd)
        msg = self.user_node.sh(excute_cmd).result()
        LOG.info(msg)
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, msg)

        LOG.info('-----step4:查看gsctl_test.dat文件   expect:有记录信息-----')
        excute_cmd = f"du -h {macro.DB_INSTANCE_PATH}/gsctl_test.dat"
        LOG.info(excute_cmd)
        msg = self.user_node.sh(excute_cmd).result()
        LOG.info(msg)
        res = msg.split()[0]
        self.assertTrue(float(res[:-1]) > 0)

        LOG.info('--------step5:查看集群状态   expect:备机正常启动--------')
        status_cmd = f"source {macro.DB_ENV_PATH};" \
                     f"gs_om -t status --detail"
        msg = self.user_node.sh(status_cmd).result()
        self.assertTrue(len(msg.splitlines()) >= 10)
        self.assertTrue('Standby' in msg)

    def tearDown(self):
        LOG.info('--------step6:环境恢复   expect:成功--------')
        excute_cmd = f''' source {macro.DB_ENV_PATH}
                          gs_om -t status --detail
                         '''
        LOG.info(excute_cmd)
        msg = self.PrimaryNode.sh(excute_cmd).result()
        LOG.info(msg)
        is_start = self.sh_standby.restart_db_cluster()
        LOG.info(is_start)
        LOG.info('-----------删除创建文件------------------')
        excute_cmd = f"rm -rf {macro.DB_INSTANCE_PATH}/gsctl_test.dat"
        LOG.info(excute_cmd)
        msg = self.user_node.sh(excute_cmd).result()
        LOG.info(msg)
        LOG.info(f'--------{os.path.basename(__file__)} end--------')
